package com.monitor.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.monitor.entity.ErpContractRepair;
import com.monitor.service.ErpContractRepairService;
import com.monitor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 合同协议维修价格表 前端控制器
 * </p>
 *
 * @author LX
 * @since 2018-09-17
 */
@RestController
@RequestMapping("/ErpContractRepair")
@Api(description="合同协议-维修价格")
public class ErpContractRepairController {

    @Resource
    private ErpContractRepairService repairService;


    @RequestMapping("/inertOrUpdateRepair")
    @ApiOperation("添加Or修改维修价格信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<Integer> inertOrUpdateRepair(ErpContractRepair contract) {
        Result<Integer> result=new Result<Integer>();
        try{
            result.setCount(1);
            result.setData(repairService.insertOrUpdate(contract)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getRepairtById")
    @ApiOperation("根据维修价格id维修价格信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "contractId", value = "维修价格ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<ErpContractRepair> getRepairtById(Integer contractId) {
        Result<ErpContractRepair> result=new Result<ErpContractRepair>();
        try{
            result.setCount(1);
            result.setData(repairService.selectById(contractId));
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

  @RequestMapping("/getRepairtWeiXiu")
    @ApiOperation("根据维修价格id维修价格信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "contractId", value = "维修价格ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> getRepairtById(Integer custId,String name) {
        Result<Integer> result=new Result<Integer>();
        try{
            Integer price=0;
           String [] a=name.split(",");
            for (int i = 0; i < a.length; i++) {
                ErpContractRepair erpContractRepair = repairService.selectOne(new EntityWrapper<ErpContractRepair>().eq("pid", custId).eq("rep_name", a[i]));
             if (null!=erpContractRepair){


                if (erpContractRepair.getRepPrice()!=null){
                    price+=Integer.valueOf(erpContractRepair.getRepPrice());
                }
             }
            }
            result.setCount(1);
            result.setData(price);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }


    @RequestMapping("/getRepairtByConId")
    @ApiOperation("根据协议id维修价格信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "conId", value = "协议ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpContractRepair>> getRepairtByConId(Integer contractId) {
        Result<List<ErpContractRepair>>  result=new Result<List<ErpContractRepair>> ();
        try{
            result.setCount(1);
            List<ErpContractRepair> pid = repairService.selectList(new EntityWrapper<ErpContractRepair>().eq("contract_id", contractId));
            result.setData(pid);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getRepairtByConId_lxl")
    @ApiOperation("根据协议id维修价格信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "conId", value = "协议ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpContractRepair>> getRepairtByConId_lxl(Integer contractId) {
        Result<List<ErpContractRepair>>  result=new Result<List<ErpContractRepair>> ();
        try{
            result.setCount(1);
            List<ErpContractRepair> pid = repairService.selectList(new EntityWrapper<ErpContractRepair>().eq("pid", contractId));
            result.setData(pid);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getRepairById")
    @ApiOperation("根据维修价格id维修价格信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "contractId", value = "维修价格ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<ErpContractRepair> getRepairById(Integer contractId) {
        Result<ErpContractRepair> result=new Result<ErpContractRepair>();
        try{
            result.setCount(1);
            result.setData(repairService.selectById(contractId));
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/removetRepairById")
    @ApiOperation("根据维修价格id删除维修价格信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "contractId", value = "维修价格ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> removetRepairById(Integer contractId) {
        Result<Integer> result=new Result<Integer>();
        try{
            result.setCount(1);
            result.setData(repairService.deleteById(contractId)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }
    @RequestMapping("/addListRepair")
    @ApiOperation("根据维修价格id删除维修价格信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "contractId", value = "维修价格ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> addListRepair(String json) {
        Result<Integer> result=new Result<Integer>();
        try{
            ArrayList<ErpContractRepair> erpBidataLists = new ArrayList<>(JSONArray.parseArray(json, ErpContractRepair.class));

            result.setCount(1);
            result.setData(repairService.insertBatch(erpBidataLists)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

}

